var ctx=window.daijia.ctx;
var totalElements=window.daijia.totalElements;

$(document).ready(function(){
	
	var newDate = new Date();
	var startDate = new Date();
	startDate.setDate(startDate.getDate()-30);
	var startTimeStr=startDate.getFullYear()+"-"+((startDate.getMonth()+1)>9?(startDate.getMonth()+1):'0' + (startDate.getMonth()+1))+"-"+(startDate.getDate()>9?startDate.getDate():'0' + startDate.getDate())+" "+"00:00";
	var endTimeStr=newDate.getFullYear()+"-"+((newDate.getMonth()+1)>9?(newDate.getMonth()+1):'0' + (newDate.getMonth()+1))+"-"+(newDate.getDate()>9?newDate.getDate():'0' + newDate.getDate())+" "+"00:00";
	$("#startTime").val(startTimeStr);
	$("#endTime").val(endTimeStr);
	
	initPagedata(totalElements,true);
	
	$("#startTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	$("#exportStartTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#exportEndTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	//点击搜索
	$("#search_button").click(function(){
		if($("#startTime").val()=="" || $("#endTime").val()==""){
			$.scojs_message("日期不能为空", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
		if(!DATE_FORMAT.test($("#startTime").val()) || !DATE_FORMAT.test($("#endTime").val())){
		   $.scojs_message("您输入的日期格式有误，正确格式应为：2012-01-01 08:30", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		if($("#startTime").val()>$("#endTime").val()){
			 $.scojs_message("开始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
			 return false;
		}
		
		$("#dateRangeText").text($("#startTime").val()+" 至 "+$("#endTime").val());
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		
		var newTotalElements=getTotalElements();
		initPagedata(newTotalElements);
		$(".pageshow").each(function(index,element){
			$(element).find("li a").html("共 "+newTotalElements+" 条数据");
		});
	});
		
//	//点击全部
//	$("#clear_button").click(function(){
//		$("#title").val("");
//		$("#startTime").val("");
//		$("#endTime").val("");
//		$('#dateRangeCalendar').DatePickerClear();
//		$('#dateRangeText').text("选择时间");
//		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
//		
//		var newTotalElements=loadData(1);
//		initPagedata(newTotalElements,false);
//		$(".pageshow").each(function(index,element){
//			$(element).find("li a").html("共 "+newTotalElements+" 条数据");
//		});
//	});
	
	//导出。。。。。。。。
	
	//导出数量
	$("#allAmount").click(function(){
		if($(this).is(':checked')){
			$("#amounts").attr("disabled",true);
		}
		else{
			$("#amounts").attr("disabled",false);
		}
	});
	
	//开始导出
	$("#export_button").click(function(){
		
		if(!$("#allAmount").is(':checked')){
			var amounts=$("#amounts").val();
			if(amounts==""){
				$.scojs_message("请输入导出数量", $.scojs_message.TYPE_ERROR);
				return false;
			}
			else if(amounts<=0){
				$.scojs_message("导出数量最少为1条", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		window.location=ctx+"/chart/exportActivity?"+$("#ExportForm").serialize();
	});
	
});

function loadData(page){
	
	if($('#loadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='loadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$(".table tbody").append(backlayer);
		$('#loadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
	
	var off_set=(page-1)*20;
	var newcontent = '';
	var newTotalElements=0;
	
	$.ajax({
        url : ctx+'/chart/activityData',
        type:'GET',
        async:true,
        dataType : 'json',
        data : {
        	"pager.offset":off_set,
        	"title":$("#title").val(),
        	"startTime":$("#startTime").val(),
        	"endTime":$("#endTime").val(),
        	"companyId":$("#companyId").val()
        },
        success : function(result){
        	if(result.totalElements>0){
        newcontent+="<tbody style='display:none;'>";
        		$(result.content).each(function(index,data){
        			var activityType="返劵";
        			if(data.activityType=="cash"){
        				activityType="返现";
        			}
	    newcontent+="<tr>"+
						"<td>"+data.company.abbreviation+"</td>"+
						"<td>"+data.title+"</td>"+
						"<td>"+activityType+"</td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.joinAmount+"</span></td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.totalMoney+"</span></td>"+
					"</tr>";
        		});
        newcontent+="</tbody>";
        	}
        	$('#loadblacklayer').remove();
        	$(".table tbody").remove();
        	$(".table").append(newcontent);
        	$(".table tbody").show(300);
    		newTotalElements=result.totalElements;
        }
	});
	
	return newTotalElements;
}

//分页初始化
function initPagedata(total){
	$(".pagedata").paging(total, {
		format: '[< ncnnn >]',
		perpage: 20,
		onSelect: function (page) {
			loadData(page);
	    	return false;
		},
		onFormat: function (type) {
			switch (type) {
			case 'block': // n and c
				if (!this.active)
					return '';
				else if (this.value != this.page)
					return '<li><a href="#">' + this.value + '</a></li>';
				return '<li class="active"><a href="#">' + this.value + '</a></li>';
			case 'next': // >
				return '<li><a href="#">&rsaquo;</a><li>';
			case 'prev': // <
					return '<li><a href="#">&lsaquo;</a><li>';
			case 'first': // [
					return '<li><a href="#">&laquo;</a><li>';
			case 'last': // ]
					return '<li><a href="#">&raquo;</a></li>';
			}
		}
	});
}

function getTotalElements(){
	
	$.ajax({
        url : ctx+'/chart/activityData',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"companyId":$("#companyId").val(),
        	"title":$("#title").val()
        },
        success : function(result){
        	newTotalElements=result.totalElements;
        }
	});
	
	return newTotalElements;
}

